<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Cache;
use app\index\model\traits\ModelHasTime;

class KnowUserTraining extends Model
{
	use ModelHasTime;

	public function getCourseUserTraing($param)
	{
		$user = user();

		return self::field('id,content,time,grade')
					->where('course_id', $param['id'])
					->where('user_id', $user['id'])
					->order('time desc')
					->select();
	}

	public function getUserTrainIds($param)
	{
		$user = $param['user'] ?: user();

		return self::field('id,content,time,grade')
					->when($param['id'], function($query) use ($param){
						$query->where('course_id', 'in', $param['id']);
					})
					->where('user_id', $user['id'])
					->order('time desc')
					->column('course_id,grade');
	}

	public function getAllData($param)
	{
		return self::alias('train')
					->field('id,content,time,grade,user_id,nav_name,type_name,s_name,course_name,know_nickname,know_phone,train.nav_id,train.type_id,train.s_id')
					->when($param['nav_id'], function($query) use ($param){
						$param['nav_id'] = is_array($param['nav_id']) ?: [$param['nav_id']];
						$query->where('train.nav_id', 'in', $param['nav_id']);
					})
					->when($param['type_id'], function($query) use ($param){
						$query->where('train.type_id', $param['id']);
					})
					->when($param['s_id'], function($query) use ($param){
						$query->where('train.s_id', $param['id']);
					})
					->when($param['course_id'], function($query) use ($param){
						$query->where('train.course_id', $param['course_id']);
					})
					->join('know_user','know_user.know_user_id=train.user_id')
					->join('course','course.course_id=train.course_id')
					->join('nav','nav.nav_id=train.nav_id')
					->join('type','type.type_id=train.type_id')
					->join('specific','specific.s_id=train.s_id')
					->order('status asc,time desc')
					->paginate(20,false,['query'=>$param]);
	}


	public function getOneData($param)
	{
		return self::alias('train')
					->field('id,content,grade,train.course_id,user_id,nav_name,type_name,s_name,course_name,know_nickname,know_phone,train.nav_id,train.type_id,train.s_id')
					->where('id',$param['id'])
					->join('know_user','know_user.know_user_id=train.user_id')
					->join('course','course.course_id=train.course_id')
					->join('type','train.type_id=type.type_id')
					->join('specific','train.s_id=specific.s_id')
					->join('nav','train.nav_id=nav.nav_id')
					->find();
	}
}